Imports DAL
Imports MyClasses
Imports System.Configuration

Public Class frmMiscCd
    Public _intMiscID As String
    'Public strCodeType As String = ""

    Private Function isUpdate() As Boolean
        Try
            If Trim(cmbType.Text) <> Trim(cmbType.Tag) Or Trim(cmbViewDesc.Text) <> Trim(cmbViewDesc.Tag) _
            Or Trim(txtID.Text) <> Trim(txtID.Tag) Or Trim(txtDescp.Text) <> Trim(txtDescp.Tag) Then
                Return True
            Else : Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub frmMiscCd_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Modified By:-Vinit(20071229)
        If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
            If isUpdate() Then
                Select Case BCommon.ShowMsgError(10081, "YNC")
                    Case Windows.Forms.DialogResult.Yes
                        If ValidateData() Then
                            If btnSave.Visible Then
                                SaveData()
                            Else
                                UpdateData()
                            End If
                        Else
                            e.Cancel = True
                        End If
                    Case Windows.Forms.DialogResult.No

                    Case Windows.Forms.DialogResult.Cancel
                        e.Cancel = True
                End Select
            End If
        End If
    End Sub

    Private Sub frmMiscCd_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Master Codes")
        e.Cancel = True
    End Sub

    Private Sub frmMiscCd_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Master Codes")
        End If
    End Sub

    Private Sub frmMiscCd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call BCommon.SetFormStandards(Me)
        Call DisplayData()
        lblLastCdID.Visible = False
        Call LastCDID()     'Divya 21/04/08
    End Sub

    'Divya 21/04/08
    Private Sub LastCDID()
        Dim strCodeID As String
        Try
            strCodeID = BMiscCode.GetLastCd(cmbType.SelectedValue)
            If IsNumeric(strCodeID) Then
                lblLastCdID.Visible = True
                lblLastCdID.Text = "(Last CodeID:" & strCodeID & ")"
            Else
                lblLastCdID.Visible = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "LastCDID")
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.Cursor = Cursors.WaitCursor
        If ValidateData() Then
            Call SaveData()
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub SetComboValue()
        Dim dtsCodes As New DataSet
        Try
            dtsCodes = BMiscCode.GetMasterCodes
            'cmbType.Items.Insert(0, " ")
            cmbType.DataSource = dtsCodes.Tables(0)
            cmbType.DisplayMember = dtsCodes.Tables(0).Columns(1).ToString()
            cmbType.ValueMember = dtsCodes.Tables(0).Columns(0).ToString()
            cmbType.Tag = cmbType.Text
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetComboValue")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    'Divya 01/10/08
    Private Sub ViewCodeIdDesc()
        Try
            cmbViewDesc.DataSource = BMiscCode.GetCodeIdDesc(cmbType.Text)
            cmbViewDesc.Splits(0).DisplayColumns(0).Width = cmbViewDesc.Width * 25 / 100
            cmbViewDesc.Splits(0).DisplayColumns(1).Width = cmbViewDesc.Width * 70 / 100
            cmbViewDesc.Columns(0).Caption = "CodeID"
            cmbViewDesc.Columns(1).Caption = "Code Description"
            cmbViewDesc.DisplayMember = "Code Description"
            cmbViewDesc.Tag = cmbViewDesc.Text
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewCodeIdDesc")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub DisplayData()
        Try
            Call ClearData()
            Call SetComboValue()

            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                btnUpt.Visible = False
                btnSave.Visible = False
                btnUpt.Enabled = False
                btnDelete.Enabled = False
                cmbViewDesc.Enabled = False
            Else
                btnUpt.Visible = False
                btnSave.Visible = True
                btnUpt.Enabled = True
                btnDelete.Enabled = False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function SaveData() As Boolean
        Dim MiscCode As New CMiscCd
        Dim blnFlag As Boolean
        Try
            MiscCode = SetData()
            'If ValidateData() = False Then Exit Function
            blnFlag = BMiscCode.Save(MiscCode)

            If blnFlag = True Then
                BCommon.ShowMsgError(3)
                DisplayData()
                Return True
            Else : Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function UpdateData() As Boolean
        Dim MiscCode As New CMiscCd
        Dim blnFlag As Boolean
        Try
            'If ValidateData() = False Then Exit Function
            MiscCode = SetData()
            blnFlag = BMiscCode.Update(MiscCode)

            If blnFlag = True Then
                BCommon.ShowMsgError(5)
                DisplayData()
                Return True
            Else : Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "UpdateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function SetData() As CMiscCd
        Dim MiscCode As New CMiscCd
        Try
            MiscCode.Code_Type = cmbType.SelectedValue
            MiscCode.Code_ID = txtID.Text
            MiscCode.Description = txtDescp.Text
            Return MiscCode
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Function ValidateData() As Boolean
        Dim dtCodeId As New DataTable
        Dim count As Integer
        Try
            If cmbType.ValueMember = "" Then
                BCommon.ShowMsgError(10003)
                cmbType.Focus()
                Return False
            End If

            If Trim(txtID.Text) = "" Then
                BCommon.ShowMsgError(10004)
                txtID.Focus()
                Return False
            End If

            'Divya 01/10/2008
            If btnSave.Visible = True Then
                dtCodeId = BMiscCode.GetCodeIdDesc(cmbType.Text)
                If dtCodeId.Rows.Count > 0 Then
                    For count = 0 To dtCodeId.Rows.Count - 1
                        If Trim(txtID.Text) = dtCodeId.Rows(count).Item("CodeId") Then
                            BCommon.ShowMsgError(10006)
                            txtID.Text = ""
                            txtID.Focus()
                            Return False
                        End If
                    Next
                End If
            End If
            If Trim(txtDescp.Text) = "" Then
                BCommon.ShowMsgError(10005)
                txtDescp.Focus()
                Return False
            End If

            Return True

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
            Return False
        End Try
    End Function

    Private Sub ClearData()
        cmbType.DataSource = Nothing
        txtID.Text = ""
        txtDescp.Text = ""
        txtID.Tag = ""
        txtDescp.Tag = ""
        Call ViewCodeIdDesc() '...Divya 01/10/08
    End Sub

    Private Sub cmbType_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbType.KeyUp
        If e.KeyCode = 13 Then cmbViewDesc.Focus()
    End Sub

    Private Sub btnUpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpt.Click
        Me.Cursor = Cursors.WaitCursor
        If ValidateData() Then
            Call UpdateData()
        End If
        txtID.Enabled = True
        cmbType.Enabled = True
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim form As New frmMasterBrowse
        Try
            Me.Cursor = Cursors.WaitCursor
            form.strSearch = "Master Code"
            'Rahul_ITS@20072612
            '<<START
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            '>>END
            form.ShowDialog()
            _intMiscID = form.strRecordNo
            If _intMiscID <> "0" Then
                Call ClearData()
                Call SetComboValue()
                Call ViewData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ViewData()
        Dim dtsCodes As New DataSet
        Try
            dtsCodes = BMiscCode.GetMiscCodes(_intMiscID)
            With dtsCodes.Tables(0).Rows(0)
                cmbType.Text = .Item(1)       'Code Type
                cmbType.Tag = .Item(1)        'Code Type
                cmbType.Enabled = False
                txtID.Text = .Item(2)         'CodeID
                txtID.Tag = .Item(2)          'CodeID
                txtID.Enabled = False
                txtDescp.Text = .Item(3)      'CodeDesc
                txtDescp.Tag = .Item(3)       'CodeDesc

                'Divya@20/03/2008 : Added regarding assignment of Roles & Privilege
                'btnDelete.Enabled = True
                Call AssignPrivilege()
                '>>End Divya

                'Enabled the Update Button on the basis of Financial Year
                If ConfigurationManager.AppSettings("FormMode") = "R" Then
                    txtDescp.Enabled = False
                    btnUpt.Visible = False
                    btnDelete.Enabled = False
                    cmbViewDesc.Enabled = False
                Else
                    txtDescp.Enabled = True
                    btnUpt.Visible = True
                    btnDelete.Enabled = True
                    cmbViewDesc.Enabled = True
                End If

                btnSave.Visible = False
                If .Item(4) = "N" Then        'Check whether the field is updatable or not.
                    'MessageBox.Show("test")
                    BCommon.ShowMsgError("10349")
                    txtDescp.Enabled = False
                    btnUpt.Enabled = False
                    btnDelete.Enabled = False
                    'Else
                    '    btnUpt.Enabled = True
                    '    btnDelete.Enabled = True
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Divya@20/03/2008 : Added regarding assignment of Roles & Privilege
    Public Sub AssignPrivilege()
        Dim intCount As Integer
        Dim dtPriv As New DataTable
        Try
            dtPriv = BRole.GetRolePriv(ConfigurationManager.AppSettings("UserID"))
            If dtPriv.Rows.Count > 0 Then
                For intCount = 0 To dtPriv.Rows.Count - 1
                    If dtPriv.Rows(intCount).Item("PrID") = 7 Then
                        btnDelete.Enabled = True
                    End If
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "AssignPrivilege")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            grpbControls.BringToFront()
            DisplayData()
            cmbType.Enabled = True
            txtID.Enabled = True
            txtDescp.Enabled = True
            btnSave.Enabled = True
            btnUpt.Enabled = True
            btnDelete.Enabled = False
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDescp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescp.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
            btnUpt.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtID.KeyDown
        If e.KeyCode = Keys.Enter Then txtDescp.Focus()
    End Sub

    Private Sub cmbType_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbType.Leave
        Call LastCDID()
    End Sub
    '...Divya 01/10/08
    Private Sub cmbType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbType.SelectedIndexChanged
        Call ViewCodeIdDesc()
        'Divya 21/04/08
        If cmbType.SelectedIndex <> -1 AndAlso cmbType.SelectedIndex <> 0 Then
            Call LastCDID()
        End If
    End Sub

    Private Sub cmbViewDesc_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbViewDesc.KeyUp
        If e.KeyCode = 13 Then txtID.Focus()
    End Sub
    'Divya 07/02/2008
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim dtMiscCode As New DataTable
        Dim intRowCount, intColCount As Integer
        Dim blnGotRecrod As Boolean
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim strCode As String = cmbType.SelectedValue
            Select Case strCode
                Case "SCHEDULETYPE"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "CARDTYPE"
                    dtMiscCode = BMiscCode.GetCardType()
                Case "DOCTOR_SPECIALITY"
                    dtMiscCode = BMiscCode.GetDoctrSpeciality(strCode)
                Case "EMPQUALIFICATION"
                    dtMiscCode = BMiscCode.GetEmpQual()
                Case "QUALIFICATION"
                    dtMiscCode = BMiscCode.GetDoctrSpeciality(strCode)
                Case "DRUGACTION"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "ADDRESS_TYPE"
                    dtMiscCode = BMiscCode.GetAddressType()
                Case "LOCTYPE"
                    dtMiscCode = BMiscCode.GetLocType()
                Case "SALEUNIT"
                    dtMiscCode = BMiscCode.GetPurSale(strCode)
                Case "PURPACK"
                    dtMiscCode = BMiscCode.GetPurSale(strCode)
                Case "GENERICNAME"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "ITEMGRADE"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "ITEMFORM"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "ITEM_CATEGORY"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                Case "ITEM_TYPE"
                    dtMiscCode = BMiscCode.GetFromMpItem(strCode)
                    'Case "UNITS"
                    '    'MessageBox.Show("This Record can not be Deleted. ")
                    '    BCommon.ShowMsgError(10396)
            End Select

            If dtMiscCode.Rows.Count > 0 Then
                For intColCount = 0 To dtMiscCode.Columns.Count - 1
                    For intRowCount = 0 To dtMiscCode.Rows.Count - 1
                        If txtID.Text = dtMiscCode.Rows(intRowCount).Item(intColCount) Then blnGotRecrod = True
                    Next
                Next
                If blnGotRecrod = True Then
                    'MessageBox.Show("Can't Delete, Master Code is in Use.", "Pharmacy")
                    BCommon.ShowMsgError(10368)
                Else
                    BMiscCode.DeleteMiscCode(txtID.Text, cmbType.SelectedValue)
                    'MessageBox.Show("Master Code Deleted Successfully. ", "Pharmacy")
                    BCommon.ShowMsgError(10369)
                End If
            End If
            Call DisplayData()
            cmbType.Enabled = True
            txtID.Enabled = True
            txtDescp.Enabled = True
            btnUpt.Enabled = True
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbViewDesc_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbViewDesc.Leave
        cmbViewDesc.SelectedIndex = -1
    End Sub
    'END Divya

End Class